package com.agh.portal.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.agh.portal.data.DataComponent;
import com.agh.portal.domain.Ad;
import com.agh.portal.domain.Answer;
import com.agh.portal.domain.Comment;
import com.agh.portal.domain.MainPagePost;
import com.agh.portal.domain.Question;
import com.agh.portal.domain.Reply;
import com.agh.portal.service.DataService;

@RequestMapping("/")
@Controller
public class HomeController {

	@RequestMapping(produces = "text/html")
	public String show(Model uiModel){
		
		List<DataComponent> latestNewContent = new ArrayList<DataComponent>();
		latestNewContent.addAll(DataService.parseQuestionsToDataComponent(Question.findLatestQuestions( 4).getResultList()));
		latestNewContent.addAll(DataService.parseAdsToDataComponent(Ad.findLatestAds( 4).getResultList()));
		latestNewContent.addAll(DataService.parseMainPagePostsToDataComponent(MainPagePost.findMainPagePostEntries(0, 2, "creationDate", "DESC")));
		
		
		List<DataComponent> latestUserActions = new ArrayList<DataComponent>();
		latestUserActions.addAll(DataService.parseAnswersToDataComponent(Answer.findLatestAnswers( 4).getResultList()));
		latestUserActions.addAll(DataService.parseCommentsToDataComponent(Comment.findLatestComments( 4).getResultList()));
		latestUserActions.addAll(DataService.parseRepliesToDataComponent(Reply.findLatestReplys( 4).getResultList()));
		
		DataService.sortByDateDesc(latestNewContent);
		DataService.sortByDateDesc(latestUserActions);
		
		uiModel.addAttribute("latestUserActions",latestUserActions);
		uiModel.addAttribute("latestNewContent",latestNewContent);
		
		return "index";
	}
}
